package fair.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import fair.dao.CheckItemDao;
import fair.entity.PageResult;
import fair.pojo.CheckItem;
import fair.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author fair_wu
 * @version v1.0
 * @date 2021-01-17 下午 22:41
 */

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao dao;

    @Override
    public void add(CheckItem item) {
        dao.add(item);
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckItem> page = dao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void deleteById(Integer id) {
        long count = dao.findCountByItemId(id);
        if (count > 0) {
            throw new RuntimeException("当前检查项被引用，无法删除");
        }
        dao.deleteById(id);
    }

    @Override
    public CheckItem findById(Integer id) {
        return dao.findById(id);
    }

    @Override
    public void edit(CheckItem item) {
        dao.edit(item);
    }

    @Override
    public List<CheckItem> findAll() {
        return dao.findAll();
    }
}
